<template>
  <div>
    <!-- http://47.104.167.34:8107/common/upload -->
    <!-- 上传地址  字段为 file -->
    <el-upload
      action="http://47.104.167.34:8107/common/upload"
      list-type="picture-card"
      :on-preview="handlePictureCardPreview"
      :on-remove="handleRemove"
      :on-success="handleAvatarSuccess"
      :file-list="fileList"
    >
      <i class="el-icon-plus"></i>


      <div slot="file" slot-scope="{file}">
      <img
        class="el-upload-list__item-thumbnail"
        :src="file.url" alt=""
      >
      <span class="el-upload-list__item-actions">
        <span
          class="el-upload-list__item-preview"
          @click="handlePictureCardPreview(file)"
        >
          <i class="el-icon-zoom-in"></i>
        </span>
        <span
          v-if="!disabled"
          class="el-upload-list__item-delete"
          @click="handleDownload(file)"
        >
          <i class="el-icon-download"></i>
        </span>
        <span
          v-if="!disabled"
          class="el-upload-list__item-delete"
          @click="handleRemove(file)"
        >
          <i class="el-icon-delete"></i>
        </span>
      </span>
    </div>





    </el-upload>
    <el-dialog :visible.sync="dialogVisible">
      <img width="100%" :src="dialogImageUrl" alt="" />
    </el-dialog>
  </div>
</template>

<script>
import axios from 'axios'
import download from 'downloadjs'
export default {
  data() {
    return {
      dialogImageUrl: "",
      dialogVisible: false,
      disabled: false,
      fileList: [
        {
        name: 'food.jpeg', 
      url: 'https://fuss10.elemecdn.com/3/63/4e7f3a15429bfda99bce42a18cdd1jpeg.jpeg?imageMogr2/thumbnail/360x360/format/webp/quality/100'
      }, 
      {
        name: 'food2.jpeg', 
      url: 'https://fuss10.elemecdn.com/3/63/4e7f3a15429bfda99bce42a18cdd1jpeg.jpeg?imageMogr2/thumbnail/360x360/format/webp/quality/100'
      }
      ],
    };
  },
  methods: {
    handleDownload(file){
      console.log(file);
      axios({
        url:file.url,
        methid:"GET",
        responseType:"blob"
      }).then((res) =>{
        console.log(res);
        download(res.data,"我的图片.jpg","image/jpeg")
        // 第一个参数 是二进制文件流
        // 第二个参数 图片名字
        // 第三个参数 下载的文件类型
      })
    },
    handleRemove(file, fileList) {
      console.log(file, fileList);
      for(let i=0;i<this.fileList.length;i++){
        if(this.fileList[i].url == file.url){
          this.fileList.splice(i,1)
        }
      }
    },
    handlePictureCardPreview(file) {
      this.dialogImageUrl = file.url;
      this.dialogVisible = true;
    },
    handleAvatarSuccess(res, file) {
        console.log('res',res);
        this.fileList.push(file)
        console.log('file',file);
      // this.imageUrl = URL.createObjectURL(file.raw);
    },
  },
};
</script>

<style>
</style>